﻿


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using NicoView.Model.Service;
using NicoView.Model.Share.Speech.Learn;
using NicoView.Model.Platform.Speachers;
using NicoView.Model.Share.Speech;

namespace NicoView.Model.Speech
{
    public sealed class  NicoViewSpeacherApplication:NicoViewObservableObject
    {
        private NicoViewSpeacherApplication()
        {

            LearnDictionary = new LearnDictionary();
            this.RegexLearnDictionary = new RegexLearnDictionary();
            CurrentSpeacherProvider = null;
            this.ProviderList = new ObservableCollection<ISpeacherProvider>();

            this.SetProviders(NicoViewSpeachersFactory.Current.CreateSpaeckers(this.RegexLearnDictionary, this.LearnDictionary));
            

        }


        public LearnDictionary LearnDictionary { get; private set; }

        public RegexLearnDictionary RegexLearnDictionary { get; private set; }



        private ISpeacherProvider _currentSpeacherProvider;
        internal ISpeacherProvider CurrentSpeacherProvider 
        {
            get
            {
                return _currentSpeacherProvider;
            }
            set
            {
                if (_currentSpeacherProvider == value) return;
                _currentSpeacherProvider = value;
                RaisePropertyChanged("CurrentSpeacherProvider");
            }
        }

        public ObservableCollection<ISpeacherProvider> ProviderList { get; set; }

        internal void SetProviders(IEnumerable<ISpeacherProvider> providers)
        {

            foreach (var provider in providers)
            {
                this.ProviderList.Add(provider);
            }

            if (this.ProviderList.Count > 0) this.CurrentSpeacherProvider = this.ProviderList.First();
            
        }


        public void SetCurrentSpeacher(string name)
        {
            var rslt = from speacher in ProviderList
                       where speacher.Name == name
                       select speacher;
            if (rslt.Count() > 0) CurrentSpeacherProvider = rslt.First();
        }

        private void _setLearnDictionary(ICollection<Learnword> words)
        {
            LearnDictionary.Set(words);
        }

        private void _setRegexLearnDictionary(ICollection<Learnword> words)
        {
            RegexLearnDictionary.Set(words);
        }

        private void _initalize()
        {
            RegexLearnDictionary.Initalize();
        }
        
        

        public void Set(SerializableSpeechInfo info)
        {
            this.SetCurrentSpeacher(info.CurrentSpeacherName);
            this._setLearnDictionary(info.Words);
            this._setRegexLearnDictionary(info.RegexWords);
            if (info.IsFirst) _initalize();

        }

        public SerializableSpeechInfo AsSave()
        {
            var info = new SerializableSpeechInfo();
            info.IsFirst = false;
            info.Words = LearnDictionary.Values;
            info.RegexWords = RegexLearnDictionary.Values;
            if (CurrentSpeacherProvider != null) info.CurrentSpeacherName = CurrentSpeacherProvider.Name;
            return info;
        }


        public NicoSpeechServiceApplication CreateService()
        {
            return new NicoSpeechServiceApplication(this);
        }

        public static readonly NicoViewSpeacherApplication Current = new NicoViewSpeacherApplication();

        
        
    }
}
